<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
if (isset($_POST['login'])) {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $sid=trim($_POST['sid']);
    if (($username=='')||($password=='')) {
        header('refresh:3;url=login.php');
        echo "改用户名或密码不能为空，3秒后跳转到登录页面";
        exit;
    } elseif (($username!='test')||($password!='123')) {
        //用户名或密码错误
        header('refresh:3;url=login.php');
        echo "用户名或密码错误，3秒后跳转到登录页面";
        exit;
    } elseif (($username=='test')&&($password=='123')) {
        //登录成功将信息保存到session中
        $_SESSION['username']=$username;
        $_SESSION['islogin']=1;

        // 写session_id到文件sid.txt中 注意这里的session_id是电脑上的，要通过二维码传过来
        $myfile = fopen("sid.txt", "w") or die("Unable to open file!");
        $txt = $sid;
        fwrite($myfile, $txt);
        fclose($myfile);

        // 写用户名到文件user.txt中
        $myfile = fopen("user.txt", "w") or die("Unable to open file!");
        $txt = $username;
        fwrite($myfile, $txt);
        fclose($myfile);
        
        echo "<b>登录成功，请在电脑上操作！</b>";
    }
}
